package com.appcheck.recording.listener.impl;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.appcheck.recording.ListenerManager;
import com.appcheck.recording.WidgetHelper;
import com.appcheck.recording.action.impl.ItemClickAction;
import com.appcheck.recording.listener.AbstractListener;
import com.appcheck.recording.utils.Logger;

public class ItemClickListener extends AbstractListener implements AdapterView.OnItemClickListener{

	private int b;
	
	public ItemClickListener(View paramView){
		super(paramView, AdapterView.class, AdapterView.OnItemClickListener.class);
	}
	
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if(this.b++ > 1){
			return;
		}
		//Logger.DEBUG("onItemClick");
		ItemClickAction itemClickAction = new ItemClickAction();
		//itemClickAction.setLine(position - parent.getFirstVisiblePosition());
		//这里改为记录相对于ApdaterView的position,以前是减去第一次出现的位置,position和id是一样的
		itemClickAction.setLine(position);
		Logger.INFO("onItemClick position = " + position + ", id = " + id);
		itemClickAction.setLongClick(false);
		itemClickAction.setParentResourceName(WidgetHelper.getResourceName(parent));
		itemClickAction.setParentResourceId(WidgetHelper.getResourceId(parent));
		ListenerManager.getListenerManager().handleAction(itemClickAction);
		if(this.listener != null){
			((AdapterView.OnItemClickListener)this.listener).onItemClick(parent, view, position, id);
		}
		this.b = 0;
	}
	
	protected final Object getListenerFromView(View paramView, Class viewClass, Class listenerClass){
		OnItemClickListener itemClickListener  = ((AdapterView)paramView).getOnItemClickListener();
		if(this.listener != null){
			Logger.INFO(new StringBuilder().append(paramView.getClass().getSimpleName()).append(" listener remembered").toString());
		}
		return itemClickListener;
	}
	
	

}
